﻿<!DOCTYPE html>
<html lang="en">
<head>
    <title>修改密码</title>
    <meta charset="UTF-8">
    <!--bootStrop css 单选框样式-->
    <link rel="stylesheet" href="resources/scripts/assets/bootstrap/css/icheck-bootstrap.css">
    <!--bootStrop css 主样式-->
    <link rel="stylesheet" href="resources/scripts/assets/bootstrap/css/bootstrap.min.css">
    <!--jquery js-->
    <script src="resources/scripts/assets/js/jquery-2.2.3.min.js"></script>
    <!--bootStrop js-->
    <script src="resources/scripts/assets/bootstrap/js/bootstrap.min.js"></script>
    <!--angular js-->
    <script src="resources/scripts/angular/angular.min.js"></script>
    <script>
        $(function () {
            $("#title").load("导航条.html");
        });
        //angular js
        angular.module('updatePassword', []).controller("form", function ($scope) {
            let password;

            function selectUser() {
                $.ajax({
                    url: "http://localhost:8989/hospital/User/SelectUser",
                    type: "post",
                    dataType: "json",
                    success: function (success) {
                        console.log(success);
                        password = success.password;
                    }
                });
            }

            //调用查询旧密码
            selectUser();

            //清空修改密码提示
            function empty() {
                $("#status").html("");
            }

            //验证旧密码
            $scope.$watch('ordPassword', function (newValue, oldValue) {
                //旧密码
                let ordPassword = $scope.ordPassword;
                //新输入的用户名已旧用户名作比较
                if (ordPassword != oldValue) {
                    if (ordPassword == "" || ordPassword == null) {
                        $scope.success = "请输入旧密码";
                        empty();
                        return;
                    } else if (!(/^\w{5,10}$/.test(ordPassword))) {
                        $scope.success = "请输入5到16位旧密码";
                        empty();
                        return;
                    } else if (ordPassword != password) {
                        $scope.success = "请检查旧密码是否正确";
                        empty();
                        return;
                    } else {
                        $scope.success = "";
                    }
                }
            });
            //新密码
            $scope.$watch('newPassword', function (newValue, oldValue) {
                //用户名
                let newPassword = $scope.newPassword;
                //新输入的用户名已旧用户名作比较
                if (newPassword != oldValue) {
                    if (newPassword == "" || newPassword == null) {
                        $scope.success = "请输入新密码";
                        empty();
                        return;
                    } else if (!(/^\w{5,10}$/.test(newPassword))) {
                        $scope.success = "请输入5到16位新密码";
                        empty();
                        return;
                    } else {
                        $scope.success = "";
                    }
                }
            });
            //验证新密码
            $scope.$watch('newPassword1', function (newValue, oldValue) {
                //用户名
                let newPassword = $scope.newPassword;
                //用户名
                let newPassword1 = $scope.newPassword1;
                //新输入的用户名已旧用户名作比较
                if (newPassword1 != oldValue) {
                    if (newPassword1 == "" || newPassword1 == null) {
                        $scope.success = "请再次输入新密码";
                        empty();
                        return;
                    } else if (!(/^\w{5,10}$/.test(newPassword1))) {
                        $scope.success = "请输入5到16位密码";
                        empty();
                        return;
                    } else if (newPassword1 != newPassword) {
                        $scope.success = "二次密码不一致";
                        empty();
                        return;
                    } else {
                        $scope.success = "";
                    }
                }
            });

            //保存
            $scope.save = function () {
                //获取密码
                let oldPassword = $scope.ordPassword;
                let newPassword = $scope.newPassword;
                let newPassword1 = $scope.newPassword1;

                if (oldPassword == "" || oldPassword == null) {
                    $scope.success = "请输入旧密码";
                    empty();
                    return;
                } else if (newPassword == "" || newPassword == null) {
                    $scope.success = "请输入新密码";
                    empty();
                    return;
                } else if (newPassword1 == "" || newPassword1 == null) {
                    $scope.success = "请再次输入新密码";
                    empty();
                    return;
                } else if (oldPassword != password) {
                    $scope.success = "请检查旧密码是否正确";
                    empty();
                    return;
                } else if (newPassword1 != newPassword) {
                    $scope.success = "二次密码不一致";
                    empty();
                    return;
                } else {
                    //保存修改密码
                    //查询后台数据验证旧密码是否正确
                    $.ajax({
                        url: "http://localhost:8989/hospital/User/UpdatePassword",
                        data: {
                            "newPassword": newPassword
                        },
                        type: "post",
                        dataType: "json",
                        success: function (success) {
                            if (success == true) {
                                $scope.$apply(function () {
                                    $("#status").html("<span style='color: #1abc9c'>" + "密码修改成功" + "</span>");
                                });
                                //修改密码成功清空输入框的值
                                $("input:text").val("");
                                $("input:password").val("");
                                //调用查询旧密码
                                selectUser();
                            }
                        }
                    });
                }
            }
        })
    </script>
</head>
<body ng-app="updatePassword">
<div id="title"></div>
<div class="text-center" ng-controller="form">
    <br/><br/>
    <span class="col-lg-offset-5" style="color: red">
        {{success}}
    </span>
    <span id="status"></span>
    <br/><br/>
    <form class="form-inline">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <label class="control-label">旧密码:</label>
        <input type="text" class="form-control" ng-model="ordPassword">
        <br/><br/>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <label class="control-label">新密码:</label>
        <input type="password" class="form-control" ng-model="newPassword">
        <br/><br/>
        <label class="control-label">确认新密码:</label>
        <input type="password" class="form-control" ng-model="newPassword1">
        <br/><br/><br/><br/>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="button" class="form-control btn btn-info" value="保存" style="width: 8%" ng-click="save()">
    </form>
</div>
</body>
</html>